/**
 * 
 */
package com.ims.stocks.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.ims.common.action.BaseAction;
import com.ims.common.utils.CodeValueData;
import com.ims.common.utils.propertyManager;
import com.ims.stocks.bo.UserBO;
import com.ims.stocks.model.Role;
import com.ims.stocks.model.UserDetails;
import com.ims.stocks.service.user.UserService;

/**
 * @author Achal B.I
 *
 */
public class UserAction extends BaseAction implements ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7643701038758833988L;
	private UserDetails user = new UserDetails();
	private Role role = new Role();
	private UserService userService;
	private List<CodeValueData> roleList = new ArrayList<CodeValueData>();
	private List<CodeValueData> statusList = new ArrayList<CodeValueData>();
	private List<CodeValueData> usersHeaderList = new ArrayList<CodeValueData>();
	private List<UserDetails> usersList = new ArrayList<UserDetails>();
	private List<UserDetails> usersBOList = new ArrayList<UserDetails>();
	
	private String crudType; 
	HttpServletRequest request;

	private final String ROLE = "common.user.role";
	private final String STATUS = "common.user.status";
	private final String USER_HEADER = "common.user.header";
	private String status;
	public String execute(){
		
		loadAction();
		return SUCCESS;
		
	}

	@SuppressWarnings("unchecked")
	private void loadAction() {
		roleList = propertyManager.getInstance().getListFromProp(ROLE);
		statusList = propertyManager.getInstance().getListFromProp(STATUS);
		usersHeaderList = propertyManager.getInstance().getListFromProp(USER_HEADER);
		
		String Id = getServletRequest().getParameter("id");
		setCrudType(getServletRequest().getParameter("pType"));
		if(Id!=null&&!Id.isEmpty()){
			user = (UserDetails) userService.findById(UserDetails.class, Integer.parseInt(Id) );
		}
		if(crudType!=null && crudType.equalsIgnoreCase("Delete")){
			userService.txDelete(user);
			addActionMessage("Record deleted successfully");
		}
		usersList = userService.findAll(UserDetails.class);
		for (UserDetails user : usersList) {
			usersBOList.add(new UserBO(user, user.getStatus().toString()));
		}
	}
	

	@SuppressWarnings("unchecked")
	public String addUser() throws Exception{
		role = (Role) userService.findById(Role.class, user.getRole().getRoleId());	
		user.setRole(role);
		if(crudType!=null && crudType.equalsIgnoreCase("New"))
		{
			user.setCreationDate(new Date());
		}
		if(crudType!=null && crudType.equalsIgnoreCase("Edit"))
		{
			user.setUpdationDate(new Date());
		}
		userService.txSaveOrUpdateObject(user);

		return SUCCESS;
 
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @param statusList the statusList to set
	 */
	public void setStatusList(List<CodeValueData> statusList) {
		this.statusList = statusList;
	}

	/**
	 * @return the statusList
	 */
	public List<CodeValueData> getStatusList() {
		return statusList;
	}

	/**
	 * @return the user
	 */
	public UserDetails getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(UserDetails user) {
		this.user = user;
	}

	/**
	 * @return the roleList
	 */
	public List<CodeValueData> getRoleList() {
		return roleList;
	}

	/**
	 * @param roleList the roleList to set
	 */
	public void setRoleList(List<CodeValueData> roleList) {
		this.roleList = roleList;
	}

	/**
	 * @return the usersHeaderList
	 */
	public List<CodeValueData> getUsersHeaderList() {
		return usersHeaderList;
	}

	/**
	 * @param usersHeaderList the usersHeaderList to set
	 */
	public void setUsersHeaderList(List<CodeValueData> usersHeaderList) {
		this.usersHeaderList = usersHeaderList;
	}

	/**
	 * @return the usersList
	 */
	public List<UserDetails> getUsersList() {
		return usersList;
	}

	/**
	 * @param usersList the usersList to set
	 */
	public void setUsersList(List<UserDetails> usersList) {
		this.usersList = usersList;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	public HttpServletRequest getServletRequest() {
		return this.request;
	}

	/**
	 * @return the crudType
	 */
	public String getCrudType() {
		return crudType;
	}

	/**
	 * @param crudType the crudType to set
	 */
	public void setCrudType(String crudType) {
		this.crudType = crudType;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the usersBOList
	 */
	public List<UserDetails> getUsersBOList() {
		return usersBOList;
	}

	/**
	 * @param usersBOList the usersBOList to set
	 */
	public void setUsersBOList(List<UserDetails> usersBOList) {
		this.usersBOList = usersBOList;
	}

	/**
	 * @return the role
	 */
	public Role getRole() {
		return role;
	}

	/**
	 * @param role the role to set
	 */
	public void setRole(Role role) {
		this.role = role;
	}

}
